﻿using grid_test;
using SMALLERP;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XJLSC.PM
{
    public partial class DDCCZZ : Form
    {
        DataBase db = new DataBase();
        private SqlDataReader sdr1;
        private SqlDataReader sdr2;

        public DDCCZZ()
        {
            InitializeComponent();
        }

        public delegate void CheckBoxClickedHandler(bool state);
        public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
        {
            bool _bChecked;
            public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
            {
                _bChecked = bChecked;
            }
            public bool Checked
            {
                get { return _bChecked; }
            }
        }
        class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
        {
            Point checkBoxLocation;
            Size checkBoxSize;
            bool _checked = false;
            Point _cellLocation = new Point();
            System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
            public event CheckBoxClickedHandler OnCheckBoxClicked;

            public DatagridViewCheckBoxHeaderCell()
            {
            }

            protected override void Paint(System.Drawing.Graphics graphics,
                System.Drawing.Rectangle clipBounds,
                System.Drawing.Rectangle cellBounds,
                int rowIndex,
                DataGridViewElementStates dataGridViewElementState,
                object value,
                object formattedValue,
                string errorText,
                DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                    dataGridViewElementState, value,
                    formattedValue, errorText, cellStyle,
                    advancedBorderStyle, paintParts);
                Point p = new Point();
                Size s = CheckBoxRenderer.GetGlyphSize(graphics,
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                p.X = cellBounds.Location.X +
                    (cellBounds.Width / 2) - (s.Width / 2);
                p.Y = cellBounds.Location.Y +
                    (cellBounds.Height / 2) - (s.Height / 2);
                _cellLocation = cellBounds.Location;
                checkBoxLocation = p;
                checkBoxSize = s;
                if (_checked)
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.CheckedNormal;
                else
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.UncheckedNormal;
                CheckBoxRenderer.DrawCheckBox
                (graphics, checkBoxLocation, _cbState);
            }

            protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
            {
                Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
                if (p.X >= checkBoxLocation.X && p.X <=
                    checkBoxLocation.X + checkBoxSize.Width
                && p.Y >= checkBoxLocation.Y && p.Y <=
                    checkBoxLocation.Y + checkBoxSize.Height)
                {
                    _checked = !_checked;
                    if (OnCheckBoxClicked != null)
                    {
                        OnCheckBoxClicked(_checked);
                        this.DataGridView.InvalidateCell(this);
                    }

                }
                base.OnMouseClick(e);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tabPage2.Parent = null;
            tabPage3.Parent = null;
            tabPage4.Parent = null;
            DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell bbHeader = new DatagridViewCheckBoxHeaderCell();
            colCB.HeaderCell = bbHeader;
            colCB.Name = "colCB";
            colCB.Width = 20;
            colCB.HeaderText = "";
            dataGridView4.Columns.Insert(0, colCB);
            bbHeader.OnCheckBoxClicked +=
                new CheckBoxClickedHandler(abHeader_OnCheckBoxClicked);

            DataGridViewCheckBoxColumn colCC = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
            colCC.HeaderCell = cbHeader;
            colCC.Name = "colCC";
            colCC.Width = 20;
            colCC.HeaderText = "";
            dataGridView5.Columns.Insert(0, colCC);
            cbHeader.OnCheckBoxClicked +=
                new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);

            DataGridViewCheckBoxColumn colCD = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell cdHeader = new DatagridViewCheckBoxHeaderCell();
            colCD.HeaderCell = cdHeader;
            colCD.Name = "colCD";
            colCD.Width = 20;
            colCD.HeaderText = "";
            dataGridView6.Columns.Insert(0, colCD);
            cdHeader.OnCheckBoxClicked +=
                new CheckBoxClickedHandler(cdHeader_OnCheckBoxClicked);
            if(PropertyClass.jiaose != "物控员")
            {
                tabPage4.Parent = null;
            }
        }

        private void abHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            dataGridView4.EndEdit();
            dataGridView4.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void cbHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            dataGridView5.EndEdit();
            dataGridView5.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void cdHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            dataGridView6.EndEdit();
            dataGridView6.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = "";
            strSql = "SELECT RTRIM(TA083) 项目号,RTRIM(TB003) 材料品号,RTRIM(E.MB001) 主件品号,RTRIM(E.MB002) 主件品名,RTRIM(E.MB003) 主件规格,0.00 齐套率,RTRIM(D.MB002) 材料品名,RTRIM(D.MB003) 材料规格,CAST(TB004 AS REAL) 工单需领,CAST(TB041 AS REAL) 已备料量,CAST(TB005 AS REAL) 已领数量,CAST(TB004-TB005 AS REAL) 未领数量,RTRIM(TB001) 工单单别,RTRIM(TB002) 工单单号,RTRIM(TB006) 材料工艺 FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB D ON D.MB001 = TB003 LEFT JOIN INVMB E ON TA006 = E.MB001 WHERE TA083 LIKE '%" + textBox7.Text + "%' AND TA011<>'Y' AND TA011<>'y' AND EXISTS(SELECT * FROM CMSNO WHERE NO001=TA083 AND CMSNO.UDF02='生产中') AND TB004-TB041>0 ORDER BY TA009";
            //MessageBox.Show(strSql,"提示");
            try
            {
                DataTable dt;
                dataGridView1.DataSource = "";
                dataGridView1.Refresh();

                dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                dataGridView1.DataSource = dt;
                //this.dataGridView1.Columns["已核数量"].Visible = false;
                //this.dataGridView1.Columns["主要仓库"].Visible = false;
                this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                this.dataGridView1.AllowUserToAddRows = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
                    String strSql = "";
                    progressBar1.Visible = true;
                    progressBar1.Maximum = dataGridView1.Rows.Count;//设置最大长度值
                    progressBar1.Value = 0;//设置当前值
                    progressBar1.Step = 1;//设置没次增长多少

                    //接下去进行循环运算每笔需求的供给状况，并插入到运算档里
                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                    {
                        progressBar1.Value += progressBar1.Step;
                        //先取出当行各列的值并标准化，以免出现null值
                        string clph = (this.dataGridView1.Rows[i].Cells["材料品号"].Value == null) ? "" : this.dataGridView1.Rows[i].Cells["材料品号"].Value.ToString();
                        string clpm = (this.dataGridView1.Rows[i].Cells["材料品名"].Value == null) ? "" : this.dataGridView1.Rows[i].Cells["材料品名"].Value.ToString();
                        string clgg = (this.dataGridView1.Rows[i].Cells["材料规格"].Value == null) ? "" : this.dataGridView1.Rows[i].Cells["材料规格"].Value.ToString();
                        string clgy = (this.dataGridView1.Rows[i].Cells["材料工艺"].Value == null) ? "" : this.dataGridView1.Rows[i].Cells["材料工艺"].Value.ToString();
                        double xlsl = (this.dataGridView1.Rows[i].Cells["工单需领"].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells["工单需领"].Value.ToString());
                        double ybll = (this.dataGridView1.Rows[i].Cells["已备料量"].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells["已备料量"].Value.ToString());
                        string xmh = (this.dataGridView1.Rows[i].Cells["项目号"].Value == null) ? "" : this.dataGridView1.Rows[i].Cells["项目号"].Value.ToString();
                        string gddb = (this.dataGridView1.Rows[i].Cells["工单单别"].Value == null) ? "" : this.dataGridView1.Rows[i].Cells["工单单别"].Value.ToString();
                        string gddh = (this.dataGridView1.Rows[i].Cells["工单单号"].Value == null) ? "" : this.dataGridView1.Rows[i].Cells["工单单号"].Value.ToString();             
                        double yzyl = 0.00;
                        double xmkc = 0.00;
                        double wfpkc = 0.00;
                        double wfpl = xlsl - ybll;

                if (wfpl > 0) //需领数量-已备料量大于0的才进行分配
                {
                    //1.先分配本项目多余库存              
                    try
                    {
                        strSql = "SELECT SUM(CASE WHEN TB041-TB005<=0 THEN 0 ELSE TB041-TB005 END) YZYL FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 WHERE TA083='" + xmh + "' AND TB003='" + clph + "'";
                        
                        sdr1 = db.GetDataReader1(strSql);
                        while (sdr1.Read())
                        {
                            yzyl = Convert.ToDouble(sdr1["YZYL"].ToString());
                        }
                        if (sdr1.HasRows == false)
                        {

                        }
                    }
                    catch
                    {

                    }
                    finally
                    {
                        sdr1.Close();
                    }

                    try
                    {
                        strSql = "SELECT XMKC FROM V_XMKC WHERE XMH='" + xmh + "' AND LA001='" + clph + "'";
                        sdr1 = db.GetDataReader1(strSql);
                        while (sdr1.Read())
                        {
                            yzyl = Convert.ToDouble(sdr1["XMKC"].ToString());
                        }
                        if (sdr1.HasRows == false)
                        {

                        }
                    }
                    catch
                    {

                    }
                    finally
                    {
                        sdr1.Close();
                    }

                    if (xmkc - yzyl > 0) //如果项目库存-已占用量大于0的话，把这部分多余的项目库存分配给该工单
                    {
                        if(wfpl> xmkc - yzyl)
                        {
                           strSql = "UPDATE MOCTB SET TB041=TB041+" + (xmkc-yzyl).ToString() + " WHERE TB001='" + gddb + "' AND TB002='" + gddh + "' AND TB003='" + clph + "' AND TB006='" + clgy + "'";
                            wfpl = wfpl - (xmkc - yzyl);
                        }
                        else
                        {
                           strSql = "UPDATE MOCTB SET TB041=TB041+" + wfpl.ToString() + " WHERE TB001='" + gddb + "' AND TB002='" + gddh + "' AND TB003='" + clph + "' AND TB006='" + clgy + "'";
                            wfpl = 0;
                        }

                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {
                            }
                            else
                            {
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                    }

                    if(wfpl>0)
                    {
                        try
                        {
                            strSql = " SELECT CAST(A.MC007-ISNULL(B.XMZY,0) AS REAL) 未分配库存 FROM (SELECT * FROM V_CKKC WHERE MC001='" + clph + "') A LEFT JOIN (";
                            strSql += "SELECT LA001, SUM(XMKC) XMZY FROM V_XMKC WHERE LA001 = '" + clph + "' GROUP BY LA001)  B ON A.MC001 = B.LA001";
                            sdr1 = db.GetDataReader1(strSql);
                            while (sdr1.Read())
                            {
                                wfpkc = Convert.ToDouble(sdr1["未分配库存"].ToString());
                            }
                        }
                        catch
                        {

                        }
                        finally
                        {
                            sdr1.Close();
                        }

                        if(wfpkc>0)
                        {

                            if (wfpl > wfpkc)
                            {
                               strSql = "INSERT INTO DRPLA(CREATOR,CREATE_DATE,LA001, LA002, LA005, LA011, LA012,LA013,LA014,LA015,LA016,LA017)SELECT '" + PropertyClass.OperatorCode + "',CONVERT(CHAR(20),GETDATE(),120),'" + clph + "','" + xmh + "',1," + wfpkc.ToString() + ",'" + gddb+ "' + '-' + '" + gddh + "','Y','" + PropertyClass.OperatorCode + "',CONVERT(CHAR(20),GETDATE(),120),'',''";
                            }
                            else
                            {
                                strSql = "INSERT INTO DRPLA(CREATOR,CREATE_DATE,LA001, LA002, LA005, LA011, LA012,LA013,LA014,LA015,LA016,LA017)SELECT '" + PropertyClass.OperatorCode + "',CONVERT(CHAR(20),GETDATE(),120),'" + clph + "','" + xmh + "',1," + wfpl.ToString() + ",'" + gddb + "' + '-' + '" + gddh + "','Y','" + PropertyClass.OperatorCode + "',CONVERT(CHAR(20),GETDATE(),120),'',''";
                            }

                            try
                            {
                                if (db.ExecDataBySql1(strSql) > 0)
                                {
                                }
                                else
                                {
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "软件提示");
                                throw ex;
                            }

                            if (wfpl>wfpkc)
                            {
                                strSql = "UPDATE MOCTB SET TB041=TB041+" + wfpkc.ToString() + " WHERE TB001='" + gddb + "' AND TB002='" + gddh + "' AND TB003='" + clph + "' AND TB006='" + clgy + "'";
                                wfpl = wfpl - wfpkc;
                            }
                            else
                            {
                                strSql = "UPDATE MOCTB SET TB041=TB041+" + wfpl.ToString() + " WHERE TB001='" + gddb + "' AND TB002='" + gddh + "' AND TB003='" + clph + "' AND TB006='" + clgy + "'";
                                wfpl = 0;
                            }

                            try
                            {
                                if (db.ExecDataBySql1(strSql) > 0)
                                {
                                }
                                else
                                {
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "软件提示");
                                throw ex;
                            }
                        }
                    }
                }

                //        //1.先检查项目仓库库存，并进行核销
                //        strSql = "SELECT ID,(TE003-TE004) AS MC007,TE003 FROM MRPTE WHERE TE001 ='" + ph + "' AND TE002='" + ddh + "' ORDER BY ID";
                //        try
                //        {
                //            sdr1 = db.GetDataReader1(strSql);
                //            while (sdr1.Read())
                //            {
                //                String kcid = sdr1["ID"].ToString();
                //                String ydsl = sdr1["TE003"].ToString();
                //                kcsl = Convert.ToDouble(sdr1["MC007"].ToString());
                //                if (whsl > 0 && kcsl > 0)    //未核销数大于0才执行
                //                {
                //                    if (whsl >= kcsl)   //如果未核销数量大于等于库存数量
                //                    {
                //                        strSql = "INSERT INTO MRPTD(TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013)  SELECT '" + ph + "','" + xqrq + "','" + gddb + "','" + gddh + "'," + xqsl + ",'" + ddh + "','库存','',''," + kcsl + ",'',''," + ydsl + "";

                //                        try
                //                        {
                //                            db = new DataBase();
                //                            if (db.ExecDataBySql1(strSql) > 0)
                //                            {
                //                                //更新界面已核数量
                //                                yhsl = yhsl + kcsl;
                //                                this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["已核数量"].Index].Value = yhsl.ToString();
                //                                //更新供给方已核数量
                //                                strSql = "UPDATE MRPTE SET TE004=TE004+" + kcsl + " WHERE ID =" + kcid + "";
                //                                try
                //                                {
                //                                    if (db.ExecDataBySql1(strSql) > 0)
                //                                    {
                //                                        whsl = whsl - kcsl;
                //                                    }
                //                                    else
                //                                    {
                //                                    }
                //                                }
                //                                catch (Exception ex)
                //                                {
                //                                    MessageBox.Show(ex.Message, "软件提示");
                //                                    throw ex;
                //                                }

                //                            }
                //                            else
                //                            {
                //                            }
                //                        }
                //                        catch (Exception ex)
                //                        {
                //                            MessageBox.Show(ex.Message, "软件提示");
                //                            throw ex;
                //                        }

                //                    }
                //                    else     //如果未核销数量小于库存数量
                //                    {
                //                        strSql = "INSERT INTO MRPTD(TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013)  SELECT '" + ph + "','" + xqrq + "','" + gddb + "','" + gddh + "'," + xqsl + ",'" + ddh + "','库存','',''," + whsl + ",'',''," + ydsl + "";

                //                        try
                //                        {
                //                            db = new DataBase();
                //                            if (db.ExecDataBySql1(strSql) > 0)
                //                            {
                //                                //更新界面已核数量
                //                                yhsl = yhsl + whsl;
                //                                this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["已核数量"].Index].Value = yhsl.ToString();
                //                                //更新供给方已核数量
                //                                strSql = "UPDATE MRPTE SET TE004=TE004+" + whsl + " WHERE ID=" + kcid + "";
                //                                try
                //                                {
                //                                    if (db.ExecDataBySql1(strSql) > 0)
                //                                    {
                //                                        whsl = 0;
                //                                    }
                //                                    else
                //                                    {
                //                                    }
                //                                }
                //                                catch (Exception ex)
                //                                {
                //                                    MessageBox.Show(ex.Message, "软件提示");
                //                                    throw ex;
                //                                }
                //                            }
                //                            else
                //                            {
                //                            }
                //                        }
                //                        catch
                //                        {
                //                        }
                //                    }
                //                }
                //            }
                //            if (sdr1.HasRows == false)
                //            {

                //            }
                //        }
                //        catch (Exception ex)
                //        {
                //            MessageBox.Show(ex.Message, "软件提示");
                //            throw ex;
                //        }
                //        finally
                //        {
                //            sdr1.Close();
                //        }

                //        //2.再检查本订单在制
                //        strSql = "SELECT ID,TG002,TG003,TG009,TG010,(TG007-TG008) AS MC007,TG007,TG011 FROM MRPTG WHERE TG001='" + ph + "' AND TG005='" + ddh + "' ORDER BY ID";
                //        try
                //        {
                //            sdr1 = db.GetDataReader1(strSql);
                //            while (sdr1.Read())
                //            {
                //                String zzid = sdr1["ID"].ToString();
                //                String gjdb = sdr1["TG002"].ToString();
                //                String gjdh = sdr1["TG003"].ToString();
                //                String gjlx = sdr1["TG009"].ToString();
                //                String gjxh = sdr1["TG010"].ToString();
                //                String ydsl = sdr1["TG007"].ToString();
                //                String scdw = sdr1["TG011"].ToString();
                //                Double zzsl = Convert.ToDouble(sdr1["MC007"].ToString());
                //                if (whsl > 0 && zzsl > 0)    //未核销数大于0才执行
                //                {
                //                    if (whsl >= zzsl)   //如果未核销数量大于等于库存数量
                //                    {
                //                        strSql = "INSERT INTO MRPTD(TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013)  SELECT '" + ph + "','" + xqrq + "','" + gddb + "','" + gddh + "'," + xqsl + ",'" + ddh + "','" + gjlx + "','" + gjdb + "','" + gjdh + "'," + zzsl + ",'" + gjxh + "','" + scdw + "'," + ydsl + "";

                //                        try
                //                        {
                //                            db = new DataBase();
                //                            if (db.ExecDataBySql1(strSql) > 0)
                //                            {
                //                                //更新界面已核数量
                //                                yhsl = yhsl + zzsl;
                //                                this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["已核数量"].Index].Value = yhsl.ToString();
                //                                //更新供给方已核数量
                //                                strSql = "UPDATE MRPTG SET TG008=TG008+" + zzsl + " WHERE ID =" + zzid + "";
                //                                try
                //                                {
                //                                    if (db.ExecDataBySql1(strSql) > 0)
                //                                    {
                //                                        whsl = whsl - zzsl;
                //                                    }
                //                                    else
                //                                    {
                //                                    }
                //                                }
                //                                catch (Exception ex)
                //                                {
                //                                    MessageBox.Show(ex.Message, "软件提示");
                //                                    throw ex;
                //                                }

                //                            }
                //                            else
                //                            {
                //                            }
                //                        }
                //                        catch (Exception ex)
                //                        {
                //                            MessageBox.Show(ex.Message, "软件提示");
                //                            throw ex;
                //                        }

                //                    }
                //                    else     //如果未核销数量小于库存数量
                //                    {
                //                        strSql = "INSERT INTO MRPTD(TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013)  SELECT '" + ph + "','" + xqrq + "','" + gddb + "','" + gddh + "'," + xqsl + ",'" + ddh + "','" + gjlx + "','" + gjdb + "','" + gjdh + "'," + whsl + ",'" + gjxh + "','" + scdw + "'," + ydsl + "";

                //                        try
                //                        {
                //                            db = new DataBase();
                //                            if (db.ExecDataBySql1(strSql) > 0)
                //                            {
                //                                //更新界面已核数量
                //                                yhsl = yhsl + whsl;
                //                                this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["已核数量"].Index].Value = yhsl.ToString();
                //                                //更新供给方已核数量
                //                                strSql = "UPDATE MRPTG SET TG008=TG008+" + whsl + " WHERE ID=" + zzid + "";
                //                                try
                //                                {
                //                                    if (db.ExecDataBySql1(strSql) > 0)
                //                                    {
                //                                        whsl = 0;
                //                                    }
                //                                    else
                //                                    {
                //                                    }
                //                                }
                //                                catch (Exception ex)
                //                                {
                //                                    MessageBox.Show(ex.Message, "软件提示");
                //                                    throw ex;
                //                                }
                //                            }
                //                            else
                //                            {
                //                            }
                //                        }
                //                        catch
                //                        {
                //                        }
                //                    }
                //                }
                //            }
                //            if (sdr1.HasRows == false)
                //            {

                //            }
                //        }
                //        catch (Exception ex)
                //        {
                //            MessageBox.Show(ex.Message, "软件提示");
                //            throw ex;
                //        }
                //        finally
                //        {
                //            sdr1.Close();
                //        }



                //        //3.检查其他库存
                //        strSql = "SELECT ID,(TE003-TE004) AS MC007,TE003 FROM MRPTE WHERE TE001 ='" + ph + "' AND TE002='' ORDER BY ID";
                //        try
                //        {
                //            sdr1 = db.GetDataReader1(strSql);
                //            while (sdr1.Read())
                //            {
                //                String kcid = sdr1["ID"].ToString();
                //                String ydsl = sdr1["TE003"].ToString();
                //                kcsl = Convert.ToDouble(sdr1["MC007"].ToString());
                //                if (whsl > 0 && kcsl > 0)    //未核销数大于0才执行
                //                {
                //                    if (whsl >= kcsl)   //如果未核销数量大于等于库存数量
                //                    {
                //                        strSql = "INSERT INTO MRPTD(TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013)  SELECT '" + ph + "','" + xqrq + "','" + gddb + "','" + gddh + "'," + xqsl + ",'" + ddh + "','库存','',''," + kcsl + ",'',''," + ydsl + "";

                //                        try
                //                        {
                //                            db = new DataBase();
                //                            if (db.ExecDataBySql1(strSql) > 0)
                //                            {
                //                                //更新界面已核数量
                //                                yhsl = yhsl + kcsl;
                //                                this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["已核数量"].Index].Value = yhsl.ToString();
                //                                //更新供给方已核数量
                //                                strSql = "UPDATE MRPTE SET TE004=TE004+" + kcsl + " WHERE ID =" + kcid + "";
                //                                try
                //                                {
                //                                    if (db.ExecDataBySql1(strSql) > 0)
                //                                    {
                //                                        whsl = whsl - kcsl;
                //                                    }
                //                                    else
                //                                    {
                //                                    }
                //                                }
                //                                catch (Exception ex)
                //                                {
                //                                    MessageBox.Show(ex.Message, "软件提示");
                //                                    throw ex;
                //                                }

                //                            }
                //                            else
                //                            {
                //                            }
                //                        }
                //                        catch (Exception ex)
                //                        {
                //                            MessageBox.Show(ex.Message, "软件提示");
                //                            throw ex;
                //                        }

                //                    }
                //                    else     //如果未核销数量小于库存数量
                //                    {
                //                        strSql = "INSERT INTO MRPTD(TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013)  SELECT '" + ph + "','" + xqrq + "','" + gddb + "','" + gddh + "'," + xqsl + ",'" + ddh + "','库存','',''," + whsl + ",'',''," + ydsl + "";

                //                        try
                //                        {
                //                            db = new DataBase();
                //                            if (db.ExecDataBySql1(strSql) > 0)
                //                            {
                //                                //更新界面已核数量
                //                                yhsl = yhsl + whsl;
                //                                this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["已核数量"].Index].Value = yhsl.ToString();
                //                                //更新供给方已核数量
                //                                strSql = "UPDATE MRPTE SET TE004=TE004+" + whsl + " WHERE ID=" + kcid + "";
                //                                try
                //                                {
                //                                    if (db.ExecDataBySql1(strSql) > 0)
                //                                    {
                //                                        whsl = 0;
                //                                    }
                //                                    else
                //                                    {
                //                                    }
                //                                }
                //                                catch (Exception ex)
                //                                {
                //                                    MessageBox.Show(ex.Message, "软件提示");
                //                                    throw ex;
                //                                }
                //                            }
                //                            else
                //                            {
                //                            }
                //                        }
                //                        catch
                //                        {
                //                        }
                //                    }
                //                }
                //            }
                //            if (sdr1.HasRows == false)
                //            {

                //            }
                //        }
                //        catch (Exception ex)
                //        {
                //            MessageBox.Show(ex.Message, "软件提示");
                //            throw ex;
                //        }
                //        finally
                //        {
                //            sdr1.Close();
                //        }

                //        //4.检查其他订单在制


                //        //5.核销完还不够的部分,即缺工单部分直接插入核销档
                //        if (whsl > 0)
                //        {
                //            strSql = "INSERT INTO MRPTD(TD001,TD002,TD003,TD004,TD005,TD006,TD007,TD008,TD009,TD010,TD011,TD012,TD013)  SELECT '" + ph + "','" + xqrq + "','" + gddb + "','" + gddh + "'," + xqsl + ",'" + ddh + "','缺单','',''," + whsl + ",'','',0";
                //            try
                //            {
                //                if (db.ExecDataBySql1(strSql) > 0)
                //                {

                //                }
                //                else
                //                {
                //                }
                //            }
                //            catch (Exception ex)
                //            {
                //                MessageBox.Show(ex.Message, "软件提示");
                //                throw ex;
                //            }
                //        }
            }  //逐行for 循环
            progressBar1.Visible = false;

            //}

        }

            private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                string strSql = "";
            strSql = "SELECT BA001 订单单别,BA002 订单单号,BA003 订单序号,BA004 类型,BA005 单别,BA006 单号,BA007 序号,BA008 仓库编号,RTRIM(BA011) 图编,CEILING(BA009) AS 数量,BA010 备注,RTRIM(BA011) 品号 FROM MRPBA WHERE BA001='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "' AND BA002='" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "' AND BA003='" + dataGridView1.CurrentRow.Cells["序号"].Value.ToString() + "'";
            //MessageBox.Show(strSql,"提示");
          
                DataTable dt;
                dataGridView2.DataSource = "";
                dataGridView2.Refresh();

                dt = db.GetDataSet1(strSql, "MRPBA").Tables["MRPBA"];
                dataGridView2.DataSource = dt;
                this.dataGridView2.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                this.dataGridView2.AllowUserToAddRows = false;
            }
            catch 
            {
               
            }
        }

        private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string strSql = "";
            string zjph = "";
            string clph = "";
           
                if(checkBox2.CheckState==CheckState.Unchecked)
                {
                    strSql = "SELECT  TC004,RTRIM(TD001)+'-'+RTRIM(TD002)+'-'+RTRIM(TD003) AS DDH,COPTD.TD004,TD005,TD006,CAST(TD008 AS REAL) TD008,CAST(TD024 AS REAL) TD024,CAST(TD009 AS REAL) TD009,CAST(TD025 AS REAL) TD025,CAST(TD008-TD009 AS REAL) WJSL,TD010,TD013,ISNULL(COPTC.UDF01,'') 预计验货日,CAST(ISNULL(TA015,0) AS REAL) 下单数量,CAST(ISNULL(TA017,0) AS REAL) 已完成数量,CAST(KCSL AS REAL) 库存数量,CAST(DDSL AS REAL) 预计销售,CAST(YJSC AS REAL) 预计生产,CAST(YJLL AS REAL) 预计领用,CAST(KCSL-DDSL AS REAL) 平衡状况 FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN V_WLPH F ON F.TD004=COPTD.TD004 LEFT JOIN MOCTA ON TA026=TD001 AND TA027=TD002 AND TA028=TD003 AND TA006=COPTD.TD004 WHERE TD016='N' AND TD002 LIKE '%" + textBox1.Text + "%' AND (TD005 LIKE '%" + textBox5.Text + "%' OR TD006 LIKE '%" + textBox5.Text + "%') AND COPTD.TD004 LIKE '%" + textBox10.Text + "%'  ORDER BY TD010";
                }
                else
                {
                    strSql = "SELECT  TC004,RTRIM(TD001)+'-'+RTRIM(TD002)+'-'+RTRIM(TD003) AS DDH,COPTD.TD004,TD005,TD006,CAST(TD008 AS REAL) TD008,CAST(TD024 AS REAL) TD024,CAST(TD009 AS REAL) TD009,CAST(TD025 AS REAL) TD025,CAST(TD008-TD009 AS REAL) WJSL,TD010,TD013,ISNULL(COPTC.UDF01,'') 预计验货日,CAST(ISNULL(TA015,0) AS REAL) 下单数量,CAST(ISNULL(TA017,0) AS REAL) 已完成数量,CAST(KCSL AS REAL) 库存数量,CAST(DDSL AS REAL) 预计销售,CAST(YJSC AS REAL) 预计生产,CAST(YJLL AS REAL) 预计领用,CAST(KCSL-DDSL AS REAL) 平衡状况 FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN V_WLPH F ON F.TD004=COPTD.TD004 LEFT JOIN MOCTA ON TA026=TD001 AND TA027=TD002 AND TA028=TD003 AND TA006=COPTD.TD004 WHERE TD002 LIKE '%" + textBox1.Text + "%' AND (TD005 LIKE '%" + textBox5.Text + "%' OR TD006 LIKE '%" + textBox5.Text + "%') AND COPTD.TD004 LIKE '%" + textBox10.Text + "%' ORDER BY TD010";
                }

     
            try
            {
                sdr1 = db.GetDataReader1(strSql);
                DataTable dta = new DataTable("MOCTB");
                DataColumn dc1 = new DataColumn("材料品号", Type.GetType("System.String"));
                DataColumn dc2 = new DataColumn("Level", Type.GetType("System.String"));
                DataColumn dc3 = new DataColumn("LEVELS", Type.GetType("System.String"));
                DataColumn dc4 = new DataColumn("主件品号", Type.GetType("System.String"));
                DataColumn dc5 = new DataColumn("主件品名", Type.GetType("System.String"));
                DataColumn dc6 = new DataColumn("主件规格", Type.GetType("System.String"));
                DataColumn dc7 = new DataColumn("齐套数", Type.GetType("System.String"));
                DataColumn dc8 = new DataColumn("材料品名", Type.GetType("System.String"));
                DataColumn dc9 = new DataColumn("材料规格", Type.GetType("System.String"));
                DataColumn dc10 = new DataColumn("订单号", Type.GetType("System.String"));
                DataColumn dc11 = new DataColumn("客户编号", Type.GetType("System.String"));
                DataColumn dc12 = new DataColumn("订单数量", Type.GetType("System.String"));
                DataColumn dc121 = new DataColumn("赠品量", Type.GetType("System.String"));
                DataColumn dc13 = new DataColumn("已交数量", Type.GetType("System.String"));
                DataColumn dc131 = new DataColumn("赠品已交数量", Type.GetType("System.String"));
                DataColumn dc14 = new DataColumn("预计交货日", Type.GetType("System.String"));
                DataColumn dc15 = new DataColumn("预计验货日", Type.GetType("System.String"));
                DataColumn dc16 = new DataColumn("下单数量", Type.GetType("System.String"));
                DataColumn dc17 = new DataColumn("已完成数量", Type.GetType("System.String"));
                DataColumn dc18 = new DataColumn("库存数量", Type.GetType("System.String"));
                DataColumn dc19 = new DataColumn("预计销售", Type.GetType("System.String"));
                DataColumn dc20 = new DataColumn("预计生产", Type.GetType("System.String"));
                DataColumn dc21 = new DataColumn("预计领用", Type.GetType("System.String"));
                DataColumn dc22 = new DataColumn("平衡状况", Type.GetType("System.String"));
                dta.Columns.Add(dc1);
                dta.Columns.Add(dc2);
                dta.Columns.Add(dc3);
                dta.Columns.Add(dc4);
                dta.Columns.Add(dc5);
                dta.Columns.Add(dc6);
                dta.Columns.Add(dc7);
                dta.Columns.Add(dc8);
                dta.Columns.Add(dc9);
                dta.Columns.Add(dc10);
                dta.Columns.Add(dc11);
                dta.Columns.Add(dc12);
                dta.Columns.Add(dc121);
                dta.Columns.Add(dc13);
                dta.Columns.Add(dc131);
                dta.Columns.Add(dc14);
                dta.Columns.Add(dc15);
                dta.Columns.Add(dc16);
                dta.Columns.Add(dc17);
                dta.Columns.Add(dc18);
                dta.Columns.Add(dc19);
                dta.Columns.Add(dc20);
                dta.Columns.Add(dc21);
                dta.Columns.Add(dc22);

                //以上代码完成了DataTable的构架

                while (sdr1.Read())
                {
                    String strSql1 = "";
                    if (checkBox1.CheckState == CheckState.Checked)
                    {
                        strSql1 = "SELECT RTRIM(ID) 材料品号,Level,LEVELS,RTRIM(E.MB001) 主件品号,RTRIM(E.MB002) 主件品名,RTRIM(E.MB003) 主件规格,0.00 齐套数,RTRIM(D.MB002) 材料品名,RTRIM(D.MB003) 材料规格,0 工单需领,0 已备料量,0 已领数量,0 未领数量,MB064 其他项目库存 FROM Loadbom3('" + sdr1["TD004"].ToString() + "') LEFT JOIN INVMB D ON D.MB001 = ID LEFT JOIN INVMB E ON ParentID = E.MB001 LEFT JOIN V_WLPH F ON F.TD004=D.MB001 WHERE (E.MB002 LIKE '%" + textBox5.Text + "%' OR E.MB003 LIKE '%" + textBox5.Text + "%') AND D.MB025<>'P' ORDER BY aa";
                    }
                    else
                    {
                        strSql1 = "SELECT RTRIM(COPTD.TD004) 材料品号,0,0,RTRIM(E.MB001) 主件品号,RTRIM(E.MB002) 主件品名,RTRIM(E.MB003) 主件规格,0.00 齐套数,RTRIM(E.MB002) 材料品名,RTRIM(E.MB003) 材料规格,CAST(TD008 AS REAL) TD008,CAST(TD024 AS REAL) TD024,CAST(TD009 AS REAL) TD009,CAST(TD025 AS REAL) TD025,CAST(TD008-TD009 AS REAL) WJSL,TD010,TD013,ISNULL(COPTC.UDF01,'') 预计验货日,CAST(ISNULL(TA015,0) AS REAL) 下单数量,CAST(ISNULL(TA017,0) AS REAL) 已完成数量,CAST(KCSL AS REAL) 库存数量,CAST(DDSL AS REAL) 预计销售,CAST(YJSC AS REAL) 预计生产,CAST(YJLL AS REAL) 预计领用,CAST(KCSL-DDSL AS REAL) 平衡状况 FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN INVMB E ON E.MB001 = COPTD.TD004 LEFT JOIN V_WLPH F ON F.TD004=E.MB001 LEFT JOIN MOCTA ON TA026=TD001 AND TA027=TD002 AND TA028=TD003 AND TA006=COPTD.TD004 WHERE RTRIM(TD001)+'-'+RTRIM(TD002)+'-'+RTRIM(TD003)='" + sdr1["DDH"].ToString() + "'";
                    }
                    try
                    {
                        DataTable dt;
                        db = new DataBase();
                        dt = db.GetDataSet1(strSql1, "MOCTB").Tables["MOCTB"];
                        for (int i = 0; i <= dt.Rows.Count - 1; i++)
                        {
                            DataRow dr = dta.NewRow();
                            dr["材料品号"] = dt.Rows[i][0].ToString();
                            dr["Level"] = dt.Rows[i][1].ToString();
                            dr["LEVELS"] = dt.Rows[i][2].ToString();
                            dr["主件品号"] = dt.Rows[i][3].ToString();
                            dr["主件品名"] = dt.Rows[i][4].ToString();
                            dr["主件规格"] = dt.Rows[i][5].ToString();
                            dr["齐套数"] = dt.Rows[i][6].ToString();
                            dr["材料品名"] = dt.Rows[i][7].ToString();
                            dr["材料规格"] = dt.Rows[i][8].ToString();
                            dr["订单号"] = sdr1["DDH"].ToString();
                            dr["客户编号"] = sdr1["TC004"].ToString();
                            dr["订单数量"] = sdr1["TD008"].ToString();
                            dr["赠品量"] = sdr1["TD024"].ToString();
                            dr["已交数量"] = sdr1["TD009"].ToString();
                            dr["赠品已交数量"] = sdr1["TD025"].ToString();
                            dr["预计交货日"] = sdr1["TD013"].ToString();
                            dr["预计验货日"] = sdr1["预计验货日"].ToString();
                            dr["下单数量"] = sdr1["下单数量"].ToString();
                            dr["已完成数量"] = sdr1["已完成数量"].ToString();
                            dr["库存数量"] = sdr1["库存数量"].ToString();
                            dr["预计销售"] = sdr1["预计销售"].ToString();
                            dr["预计生产"] = sdr1["预计生产"].ToString();
                            dr["预计领用"] = sdr1["预计领用"].ToString();
                            dr["平衡状况"] = sdr1["平衡状况"].ToString();
                            dta.Rows.Add(dr);
                        }            
                    }

                    catch
                    {
                        MessageBox.Show(dta.Columns.Count.ToString(), "软件提示错误");
                    }

                }
                // MessageBox.Show(dta.Rows.Count.ToString(), "软件提示");
                dataGridView3.DataSource = "";
                dataGridView3.Refresh();
                if (checkBox1.CheckState == CheckState.Checked)
                {
                    DataGridViewGroupColumn group = new DataGridViewGroupColumn();
                    group.DataPropertyName = "材料品号";
                    group.HeaderText = "材料品号";
                    group.Name = "材料品号";
                    group.Width = 200;

                    this.dataGridView3.Columns.Add(group);
                    string[] GrdHeadText = new string[] { "材料品号" };
                    int[] GrdWidth = new int[] { 100, 40 };
                    string[] GrdColumnName = new string[] { "材料品号" };
                }
                dataGridView3.DataSource = dta;
                dataGridView3.Columns["Level"].Visible = false;
                dataGridView3.Columns["LEVELS"].Visible = false;
                dataGridView3.Columns["主件品号"].Visible = false;
                dataGridView3.Columns["主件品名"].Visible = false;
                dataGridView3.Columns["主件规格"].Visible = false;
                dataGridView3.Columns["齐套数"].Visible = false;
                dataGridView3.Columns["预计交货日"].Frozen = true;
                for(int i=0;i<=dataGridView3.Columns.Count-1;i++)
                {
                    dataGridView3.Columns[i].ReadOnly = true;
                }
                dataGridView3.Columns["预计验货日"].ReadOnly = false;
            }
            catch
            {

            }
            finally
            {
                sdr1.Close();
            }
            for(int i=0;i<=dataGridView3.Rows.Count-1;i++)
            {
                try
                {
                    String cpph = (dataGridView3.Rows[i].Cells["材料品号"].Value == null) ? "" : dataGridView3.Rows[i].Cells["材料品号"].Value.ToString();
                    String dddh = (dataGridView3.Rows[i].Cells["订单号"].Value == null) ? "" : dataGridView3.Rows[i].Cells["订单号"].Value.ToString();
                    //int zfsl = dataGridView3["订单号", dataGridView3.CurrentCell.RowIndex].Value.ToString().Trim().Length - 5;
                    String khdd = (dataGridView3.Rows[i].Cells["订单号"].Value == null) ? "" : dataGridView3.Rows[i].Cells["订单号"].Value.ToString();
                    strSql = " SELECT CAST(ISNULL(SUM(TA015),0) AS REAL) TA015,CAST(ISNULL(SUM(TA017),0) AS REAL) TA017 FROM MOCTA WHERE TA006='" + cpph + "' AND (RTRIM(TA027)='" + khdd + "' OR RTRIM(TA033)='" + khdd + "')";         
                    sdr1 = db.GetDataReader1(strSql);
                    while (sdr1.Read())
                    {
                        dataGridView3.Rows[i].Cells["下单数量"].Value = sdr1["TA015"].ToString();
                        dataGridView3.Rows[i].Cells["已完成数量"].Value = sdr1["TA017"].ToString();
                    }
                }
                catch
                {

                }
                finally
                {
                    sdr1.Close();
                }
            }



            if (checkBox1.CheckState == CheckState.Checked)
            {
                for (int i = 0; i < this.dataGridView3.Columns.Count; i++)
                {
                    this.dataGridView3.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                    //this.dataGridView3.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                }
            }
            try 
            {
                // this.dataGridView3.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                this.dataGridView3.AllowUserToAddRows = false;
                //this.dataGridView3.Columns["齐套数"].DefaultCellStyle.Format = "P2";
                //dataGridView3.Columns["Level"].Visible = false;
                //dataGridView3.Columns["材料品号"].Width = 300;
                //dataGridView3.Columns["材料品号"].Frozen = true;
                //dataGridView3.Columns["主件品号"].Visible = false;
                if (checkBox1.CheckState == CheckState.Checked)
                {
                    //dataGridView3.Columns["LEVELS"].Visible = false;
                }
                if (checkBox1.CheckState == CheckState.Checked)
                {
                    GruopShow1();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }

            progressBar2.Visible = true;
            progressBar2.Maximum = dataGridView3.Rows.Count;//设置最大长度值
            progressBar2.Value = 0;//设置当前值
            progressBar2.Step = 1;//设置没次增长多少
            for (int j = 0; j <= this.dataGridView3.Rows.Count - 1; j++)      //;
            {

                progressBar2.Value += progressBar2.Step;

                try
                {
                    clph = (dataGridView3.Rows[j].Cells["材料品号"].Value == null) ? "" : dataGridView3.Rows[j].Cells["材料品号"].Value.ToString();
                    zjph = (dataGridView3.Rows[j].Cells["主件品号"].Value == null) ? "" : dataGridView3.Rows[j].Cells["主件品号"].Value.ToString();
                    strSql = "SELECT CAST(ISNULL(SUM(TB004),0) AS REAL) TB004,CAST(ISNULL(SUM(TB041),0) AS REAL) TB041,CAST(ISNULL(SUM(TB005),0) AS REAL) TB005,CAST(ISNULL(SUM(TB004-TB005),0) AS REAL) WLSL FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 WHERE TA011<>'Y' AND TA011<>'y' AND TA083='" + textBox1.Text + "' AND TA006='" + zjph + "' AND TB003='" + clph + "'";

                    sdr1 = db.GetDataReader1(strSql);
                    while (sdr1.Read())
                    {
                        dataGridView3.Rows[j].Cells["工单需领"].Value = sdr1["TB004"].ToString();
                        dataGridView3.Rows[j].Cells["已备料量"].Value = sdr1["TB041"].ToString();
                        dataGridView3.Rows[j].Cells["已领数量"].Value = sdr1["TB005"].ToString();
                        dataGridView3.Rows[j].Cells["未领数量"].Value = sdr1["WLSL"].ToString();
                    }
                }
                catch
                {

                }
                finally
                {
                    sdr1.Close();
                }

                if (checkBox1.CheckState == CheckState.Checked)
                {
                    try
                    {
                        clph = (dataGridView3.Rows[j].Cells["材料品号"].Value == null) ? "" : dataGridView3.Rows[j].Cells["材料品号"].Value.ToString();
                        if (checkBox2.CheckState == CheckState.Unchecked)
                        {
                            strSql = "SELECT CAST(QTL AS REAL) AS 齐套数 FROM V_XMQTLWZ WHERE TA083='" + textBox1.Text + "' AND TA006='" + clph + "'";
                        }
                        else
                        {
                            strSql = "SELECT CAST(QTL AS REAL) AS 齐套数 FROM V_XMQTL WHERE TA083='" + textBox1.Text + "' AND TA006='" + clph + "'";
                        }

                        sdr1 = db.GetDataReader1(strSql);
                        while (sdr1.Read())
                        {
                            dataGridView3.Rows[j].Cells["齐套数"].Value = sdr1["齐套数"].ToString();
                        }
                        if (sdr1.HasRows == false)
                        {
                            if (Convert.ToDouble(dataGridView3.Rows[j].Cells["工单需领"].Value.ToString()) == 0.00)
                            {
                                dataGridView3.Rows[j].Cells["齐套数"].Value = "0";
                            }
                            else
                            {
                                dataGridView3.Rows[j].Cells["齐套数"].Value = Convert.ToDouble(dataGridView3.Rows[j].Cells["已备料量"].Value.ToString());
                            }

                        }
                    }
                    catch
                    {

                    }
                    finally
                    {
                        sdr1.Close();
                    }
                }
            }
            progressBar2.Visible = false;
            try
            {
                dataGridView3.CurrentCell = dataGridView3.Rows[0].Cells["订单号"];
            }
            catch
            {
                
            }
          

        }

        private void GruopShow1()
        {
            DataTable tb = (DataTable)this.dataGridView3.DataSource;
            if (tb == null || tb.Rows.Count == 0) return;
            //SortTreeTable(tb);
            DataTable temptable = tb;
            DataRow[] row = temptable.Select("Level=0");
            //progressBar2.Visible = true;
            //progressBar2.Maximum = row.Length;//设置最大长度值
            //progressBar2.Value = 0;//设置当前值
            //progressBar2.Step = 1;//设置没次增长多少
            for (int i = 0; i < row.Length; i++)
            {
                //progressBar2.Value += progressBar2.Step;
                //MessageBox.Show(this.dataGridView3.Rows[0].Cells[2].Value.ToString(), "软件提示");
                DataGridViewGroupCell cell = this.dataGridView3.Rows[temptable.Rows.IndexOf(row[i])].Cells[0] as DataGridViewGroupCell;//父节点
                String Parent_id = row[i]["材料品号"].ToString();

                AddRow1(temptable, cell, Parent_id);
                string s = string.Empty;

            }
            //progressBar2.Visible = false;
            this.dataGridView3.Rows[0].Selected = true;
            this.dataGridView3.Refresh();

            //DataGridViewGroupCell cell1 = this.dataGridView4.Rows[0].Cells[0] as DataGridViewGroupCell;//父节点
            //DataGridViewGroupCell cell2 = this.dataGridView4.Rows[1].Cells[0] as DataGridViewGroupCell;//父节点
            //DataGridViewGroupCell cell3 = this.dataGridView4.Rows[2].Cells[0] as DataGridViewGroupCell;//父节点
            //DataGridViewGroupCell cell4 = this.dataGridView4.Rows[3].Cells[0] as DataGridViewGroupCell;//父节点

            string ss = string.Empty;

        }

        private void AddRow1(DataTable tb, DataGridViewGroupCell cell, String Parent_id)
        {
            DataRow[] row = tb.Select("主件品号='" + Parent_id.ToString() + "' AND Level<>0");

            for (int i = 0; i < row.Length; i++)
            {
 
                DataGridViewGroupCell childcell = this.dataGridView3.Rows[tb.Rows.IndexOf(row[i])].Cells[0] as DataGridViewGroupCell;
                cell.AddChildCell(childcell);

                //try
                //{
                //  String  zjph = (dataGridView3.Rows[tb.Rows.IndexOf(row[i])].Cells["主件品号"].Value == null) ? "" : dataGridView3.Rows[tb.Rows.IndexOf(row[i])].Cells["主件品号"].Value.ToString();
                //  String  strSql = "SELECT CAST(QTL AS REAL) AS 齐套数 FROM V_XMQTL WHERE TA083='" + textBox1.Text + "' AND TA006='" + zjph + "'";
                //    sdr1 = db.GetDataReader1(strSql);
                //    while (sdr1.Read())
                //    {
                //        dataGridView3.Rows[tb.Rows.IndexOf(row[i])].Cells["齐套数"].Value = sdr1["齐套数"];
                //    }
                //}
                //catch
                //{

                //}
                //finally
                //{
                //    sdr1.Close();
                //}
                
                cell.Collapse();
                String Parent_id_new = row[i]["材料品号"].ToString();
                //MessageBox.Show(Parent_id_new.ToString(), "软件提示");
                AddRow1(tb, childcell, Parent_id_new);
            }
      
        }


        private void SortChildTreeTable1(DataTable tb, DataTable InitTb, DataRow PatentRow, String Parent_id)
        {
            DataRow[] row = InitTb.Select("主件品号='" + Parent_id.ToString().Trim() + "'");
            for (int i = 0; i < row.Length; i++)
            {
                DataRow ParentRow = row[i];
                Parent_id = row[i]["材料品号"].ToString().Trim();
                tb.Rows.Add(row[i].ItemArray);
                SortChildTreeTable1(tb, InitTb, ParentRow, Parent_id);
            }
        }

        private void dataGridView3_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
           
        }

        private void dataGridView3_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            String strSql = "";
            strSql = "SELECT RTRIM(XMH) 项目号,RTRIM(CMSNO.UDF02) 项目状态,RTRIM(LA001) 品号,RTRIM(MB002) 品名,RTRIM(MB003) 规格,MB004 单位,CAST(XMKC AS REAL) 项目库存,0 AS 调出库存,'' 主件品号 FROM V_XMKC LEFT JOIN INVMB ON LA001=MB001 LEFT JOIN CMSNO ON XMH=NO001 WHERE XMH LIKE '%" + textBox2.Text + "%' AND (LA001 LIKE '%" + textBox4.Text + "%' OR MB002 LIKE '%" + textBox4.Text + "%' OR  MB003 LIKE '%" + textBox4.Text + "%') AND XMKC>0";
            //textBox3.Text = strSql;
            try
            {
                DataTable dt;
                dataGridView4.DataSource = "";
                dataGridView4.Refresh();
                dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                dataGridView4.DataSource = dt;
                dataGridView4.Columns["单位"].Width = 30;
                dataGridView4.Columns["项目库存"].ReadOnly=true;
                dataGridView4.Columns["品号"].ReadOnly = true;
                dataGridView4.Columns["品名"].ReadOnly = true;
                dataGridView4.Columns["规格"].ReadOnly = true;
                dataGridView4.Columns["单位"].ReadOnly = true;
            }
            catch
            {
            }

                //string zjph = "";
                //try
                //{
                //    strSql = "SELECT UDF08 AS 主件品号 FROM CMSNO WHERE NO001='" + textBox2.Text + "'";
                //    sdr1 = db.GetDataReader1(strSql);
                //    while (sdr1.Read())
                //    {
                //        zjph = sdr1["主件品号"].ToString();

                //    }
                //}
                //catch
                //{

                //}
                //finally
                //{
                //    sdr1.Close();
                //}

                //strSql = "SELECT ID,ParentID,LEVELS,Level,MB002,MB003,MB004 FROM Loadbom3('" + zjph + "') LEFT JOIN INVMB ON ID=MB001 ORDER BY aa";

                //try
                //{
                //    DataTable dt;
                //    dataGridView4.DataSource = "";
                //    dataGridView4.Refresh();

                //    dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];

                //    DataGridViewGroupColumn group = new DataGridViewGroupColumn();
                //    group.DataPropertyName = "ID";
                //    group.HeaderText = "材料品号";
                //    group.Name = "ID";
                //    group.Width = 200;
                //    this.dataGridView4.Columns.Add(group);

                //    //DataGridViewTextBoxColumn group1 = new DataGridViewTextBoxColumn();
                //    //group1.DataPropertyName = "ParentID";
                //    //group1.HeaderText = "主件品号";
                //    //group1.Name = "ParentID";
                //    //group1.Width = 90;
                //    //this.dataGridView4.Columns.Add(group1);

                //    //DataGridViewTextBoxColumn group2 = new DataGridViewTextBoxColumn();
                //    //group2.DataPropertyName = "LEVELS";
                //    //group2.HeaderText = "上级id";
                //    //group2.Name = "LEVELS";
                //    //group2.Width = 200;
                //    //this.dataGridView4.Columns.Add(group2);

                //    //DataGridViewTextBoxColumn group3 = new DataGridViewTextBoxColumn();
                //    //group3.DataPropertyName = "Level";
                //    //group3.HeaderText = "阶次";
                //    //group3.Name = "Level";
                //    //group3.Width = 200;
                //    //this.dataGridView4.Columns.Add(group3);

                //    string[] GrdHeadText = new string[] { "材料品号"};
                //int[] GrdWidth = new int[] { 100,40 };
                //string[] GrdColumnName = new string[] { "ID"};
                //dataGridView4.DataSource = dt;
                ////this.dataGridView4.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                //this.dataGridView4.AllowUserToAddRows = false;

                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.Message, "软件提示");
                //    //throw ex;
                //}

                //GruopShow();
            }

        private void GruopShow()
        {
            DataTable tb = (DataTable)this.dataGridView4.DataSource;
            if (tb == null || tb.Rows.Count == 0) return;
            //SortTreeTable(tb);
            DataTable temptable = tb;
            DataRow[] row = temptable.Select("Level=0");
            for (int i = 0; i < row.Length; i++)
            {
                DataGridViewGroupCell cell = this.dataGridView4.Rows[temptable.Rows.IndexOf(row[i])].Cells[0] as DataGridViewGroupCell;//父节点
                String Parent_id = row[i]["ID"].ToString();
               
                    AddRow(temptable, cell, Parent_id);
                    string s = string.Empty;
                
            }
            this.dataGridView4.Rows[0].Selected = true;
            this.dataGridView4.Refresh();

            //DataGridViewGroupCell cell1 = this.dataGridView4.Rows[0].Cells[0] as DataGridViewGroupCell;//父节点
            //DataGridViewGroupCell cell2 = this.dataGridView4.Rows[1].Cells[0] as DataGridViewGroupCell;//父节点
            //DataGridViewGroupCell cell3 = this.dataGridView4.Rows[2].Cells[0] as DataGridViewGroupCell;//父节点
            //DataGridViewGroupCell cell4 = this.dataGridView4.Rows[3].Cells[0] as DataGridViewGroupCell;//父节点

            string ss = string.Empty;

        }

        private void AddRow(DataTable tb, DataGridViewGroupCell cell, String Parent_id)
        {
            DataRow[] row = tb.Select("ParentID='" + Parent_id.ToString() + "' AND Level<>0");
  
            for (int i = 0; i < row.Length; i++)
            {
                DataGridViewGroupCell childcell = this.dataGridView4.Rows[tb.Rows.IndexOf(row[i])].Cells[0] as DataGridViewGroupCell;
                cell.AddChildCell(childcell);
                String Parent_id_new = row[i]["ID"].ToString();
                //MessageBox.Show(Parent_id_new.ToString(), "软件提示");
                AddRow(tb, childcell, Parent_id_new);
            }
        }

        private DataTable SortTreeTable(DataTable tb)
        {
            //DataTable temp = tb.Copy();
            //tb.Clear();
            //DataRow[] row = temp.Select("Level=0");

            //for (int i = 0; i < row.Length; i++)
            //{
            //    DataRow ParentRow = row[i];
            //    int Parent_id = int.Parse(row[i]["Level"].ToString().Trim())+1;
            //    tb.Rows.Add(row[i].ItemArray);
            //    SortChildTreeTable(tb, temp, ParentRow, Parent_id);
            //}
            //tb.AcceptChanges();
            return tb;
        }

        private void SortChildTreeTable(DataTable tb, DataTable InitTb, DataRow PatentRow, String Parent_id)
        {
            DataRow[] row = InitTb.Select("ParentID='" + Parent_id.ToString().Trim() + "'");
            for (int i = 0; i < row.Length; i++)
            {
                DataRow ParentRow = row[i];
                Parent_id = row[i]["ID"].ToString().Trim();
                tb.Rows.Add(row[i].ItemArray);
                SortChildTreeTable(tb, InitTb, ParentRow, Parent_id);
            }
        }


        private void button8_Click(object sender, EventArgs e)
        {

            for (int i = 0; i <= dataGridView3.Rows.Count - 1; i++)
            {
                DataGridViewGroupCell cell = this.dataGridView3.Rows[i].Cells[0] as DataGridViewGroupCell;//父节点
                cell.Collapse();
            }

        }

        private void button7_Click(object sender, EventArgs e)
        {
           for(int i=0;i<=dataGridView3.Rows.Count-1;i++)
            {
                DataGridViewGroupCell cell = this.dataGridView3.Rows[i].Cells[0] as DataGridViewGroupCell;//父节点
                cell.Expand();
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if(textBox3.Text=="")
            {
                MessageBox.Show("转入项目号不能为空！","系统提示");
                return;
            }
            string strSql = "";
            try
            {
                strSql = "SELECT UDF08 AS 主件品号 FROM CMSNO WHERE NO001='" + textBox3.Text + "'";
                sdr1 = db.GetDataReader1(strSql);
                while (sdr1.Read())
                {

                }
                if(sdr1.HasRows==false)
                {
                    MessageBox.Show("转入项目号不不存在，请输入正确的项目号！", "系统提示");
                    return;
                }
            }
            catch
            {

            }
            finally
            {
                sdr1.Close();
            }


            for (int i=0;i<=dataGridView4.Rows.Count-1;i++)
           {
                if (Convert.ToBoolean(this.dataGridView4.Rows[i].Cells["colCB"].Value))
                {
                    int bs = 0;
                    int j = 0;
                    for (int k=0;k<=dataGridView5.Rows.Count-1;k++)
                    {
                       if (dataGridView5.Rows[k].Cells["LA001"].Value.ToString().Trim() == dataGridView4.Rows[i].Cells["品号"].Value.ToString().Trim() && dataGridView5.Rows[k].Cells["LA002"].Value.ToString().Trim() == dataGridView4.Rows[i].Cells["项目号"].Value.ToString().Trim())
                        {
                            bs += 1;
                        }
                    }
                    if (bs == 0)
                    {
                        j = dataGridView5.Rows.Add();
                        dataGridView5.Rows[j].Cells["LA001"].Value = dataGridView4.Rows[i].Cells["品号"].Value.ToString();
                        dataGridView5.Rows[j].Cells["LA002"].Value = dataGridView4.Rows[i].Cells["项目号"].Value.ToString();
                        dataGridView5.Rows[j].Cells["LA003"].Value = textBox3.Text;
                        dataGridView5.Rows[j].Cells["LA011"].Value = dataGridView4.Rows[i].Cells["调出库存"].Value.ToString();
                        dataGridView5.Rows[j].Cells["LA016"].Value = dataGridView4.Rows[i].Cells["主件品号"].Value.ToString();
                    }
                    else
                    {
                        MessageBox.Show("不能重复添加！", "系统提示");
                    }
                }
           }
        }

        private void dataGridView4_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex.Equals(this.dataGridView4.Columns["调出库存"].Index))
            {
                double dcsl = Convert.ToDouble(this.dataGridView4.CurrentRow.Cells["调出库存"].Value.ToString());
                double xmsl = Convert.ToDouble(this.dataGridView4.CurrentRow.Cells["项目库存"].Value.ToString()); 
                if (dcsl > xmsl)
                {
                    MessageBox.Show("不能超出项目库存！", "软件提示");
                    this.dataGridView4.CurrentCell.Value = xmsl.ToString();
                    return;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            String strSql = "";
            for (int i = 0; i <= dataGridView5.Rows.Count - 1; i++)
            {
                if (Convert.ToBoolean(this.dataGridView5.Rows[i].Cells["colCC"].Value))
                {
                    String bz = (dataGridView5.Rows[i].Cells["LA012"].Value == null) ? "" : dataGridView5.Rows[i].Cells["LA012"].Value.ToString();
                    strSql = "INSERT INTO DRPLA(CREATOR,CREATE_DATE,LA001,LA002,LA005,LA011,LA012,LA013,LA014,LA015,LA016,LA017) SELECT '" + PropertyClass.OperatorCode + "',CONVERT(CHAR(20),GETDATE(),120),'" + dataGridView5.Rows[i].Cells["LA001"].Value.ToString() + "','" + dataGridView5.Rows[i].Cells["LA002"].Value.ToString() + "',-1," + dataGridView5.Rows[i].Cells["LA011"].Value.ToString() + ",'" + bz + "','N','','','" + dataGridView5.Rows[i].Cells["LA016"].Value.ToString() + "','" + dataGridView5.Rows[i].Cells["LA003"].Value.ToString() + "'";

                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                        }
                        else
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }

                    strSql = "INSERT INTO DRPLA(CREATOR,CREATE_DATE,LA001,LA002,LA005,LA011,LA012,LA013,LA014,LA015,LA016,LA017)SELECT '" + PropertyClass.OperatorCode + "',CONVERT(CHAR(20),GETDATE(),120),'" + dataGridView5.Rows[i].Cells["LA001"].Value.ToString() + "','" + dataGridView5.Rows[i].Cells["LA003"].Value.ToString() + "',1," + dataGridView5.Rows[i].Cells["LA011"].Value.ToString() + ",'" + bz + "','N','','','" + dataGridView5.Rows[i].Cells["LA016"].Value.ToString() + "','" + dataGridView5.Rows[i].Cells["LA002"].Value.ToString() + "'";
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                        }
                        else
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                }
            }
            dataGridView5.Rows.Clear();
            dataGridView4.DataSource = "";
            dataGridView4.Refresh();
            MessageBox.Show("物料调拨成功！", "软件提示");
        }

        private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = dataGridView5.Rows.Count - 1; i >=0; i--)
            {
                if (Convert.ToBoolean(this.dataGridView5.Rows[i].Cells["colCC"].Value))
                {
                    dataGridView5.Rows.RemoveAt(i);
                }
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            string strSql = "";
            for (int i=0;i<=dataGridView3.Rows.Count-1;i++)
            {
                double xlsl = (dataGridView3.Rows[i].Cells["需领数量"].Value == null) ? 0.00 : Convert.ToDouble(dataGridView3.Rows[i].Cells["需领数量"].Value.ToString());
                double wlsl = (dataGridView3.Rows[i].Cells["未领数量"].Value == null) ? 0.00 : Convert.ToDouble(dataGridView3.Rows[i].Cells["未领数量"].Value.ToString());
                double xmkc = (dataGridView3.Rows[i].Cells["项目库存"].Value == null) ? 0.00 : Convert.ToDouble(dataGridView3.Rows[i].Cells["项目库存"].Value.ToString());
                double ybll = (dataGridView3.Rows[i].Cells["已备料量"].Value == null) ? 0.00 : Convert.ToDouble(dataGridView3.Rows[i].Cells["已备料量"].Value.ToString());
                String clph = (dataGridView3.Rows[i].Cells["材料品号"].Value == null) ? "" : dataGridView3.Rows[i].Cells["材料品号"].Value.ToString();
                double yzyl = 0.00;
                if (xlsl-ybll>0) //需领数量-已备料量大于0的才进行分配
                {
                    //1.先分配本项目多余库存              
                    try
                    {
                        strSql = "SELECT SUM(CASE WHEN TB041-TB005<=0 THEN 0 ELSE TB041-TB005 END) YZYL FROM MOCTB LEFT JOIN MOCTA ON TB001=TA001 AND TB002=TA002 WHERE TA083='" + textBox1.Text + "' AND TB003='" + clph + "'";
                        sdr1 = db.GetDataReader1(strSql);
                        while (sdr1.Read())
                        {
                            yzyl = Convert.ToDouble(sdr1["YZYL"].ToString());
                        }
                        if (sdr1.HasRows == false)
                        {

                        }
                    }
                    catch
                    {

                    }
                    finally
                    {
                        sdr1.Close();
                    }

                    if(xmkc-yzyl>0) //如果项目库存-已占用量大于0的话，把这部分多余的项目库存分配给该工单
                    {

                    }

                }
            }
        }

        private void 借料ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string x1 = "";
            string x2 = "";
            double x3 = 0;
            string x4 = "";
            string x5 = "";

            try
            {

                x1 = (dataGridView3["材料品号", dataGridView3.CurrentCell.RowIndex].Value==null)?"":dataGridView3["材料品号", dataGridView3.CurrentCell.RowIndex].Value.ToString();
                x3 = (dataGridView3["下单数量", dataGridView3.CurrentCell.RowIndex].Value == null)?0.00:Convert.ToDouble(dataGridView3["下单数量", dataGridView3.CurrentCell.RowIndex].Value.ToString());



                x4 = "查看物料BOM状况";
                MXCX xx = new MXCX(x1, x2, x3, x4, x5);
                CHAXUN f = new CHAXUN();
                f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                f.Show(this);
            }
            catch
            {

            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            String strSql = "";
            strSql = "SELECT A.CREATOR 申请账号,A.CREATE_DATE 申请时间,A.LA001 材料品号,B.MB002 材料品名,B.MB003 材料规格,A.LA002 转入项目号,CAST(A.LA011 AS REAL) 数量,A.LA016 主件品号,C.MB002 主件品名,C.MB003 主件规格,D.LA002 转出项目号 FROM DRPLA A LEFT JOIN INVMB B ON LA001=B.MB001 LEFT JOIN INVMB C ON A.LA016=C.MB001 LEFT JOIN DRPLA D ON A.LA001=D.LA001 AND A.LA016=D.LA016 AND A.LA011=D.LA011 AND A.LA005=D.LA005*(-1) AND A.LA017=D.LA002 WHERE A.LA013='N' AND A.LA005=1 AND A.LA002 LIKE '%" + textBox9.Text + "%' AND D.LA002 LIKE '%" + textBox8.Text + "%' AND A.CREATOR LIKE '%" + textBox6.Text + "%'";
            //textBox3.Text = strSql;
            try
            {
                DataTable dt;
                dataGridView6.DataSource = "";
                dataGridView6.Refresh();
                dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                dataGridView6.DataSource = dt;
                //dataGridView4.Columns["单位"].Width = 30;
                //dataGridView4.Columns["项目库存"].ReadOnly = true;
                //dataGridView4.Columns["品号"].ReadOnly = true;
                //dataGridView4.Columns["品名"].ReadOnly = true;
                //dataGridView4.Columns["规格"].ReadOnly = true;
                //dataGridView4.Columns["单位"].ReadOnly = true;
            }
            catch
            {
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            String strSql = "";
            for (int i = 0; i <= dataGridView6.Rows.Count - 1; i++)
            {
                if (Convert.ToBoolean(this.dataGridView6.Rows[i].Cells["colCD"].Value))
                {
                    String clph = (dataGridView6.Rows[i].Cells["材料品号"].Value == null) ? "" : dataGridView6.Rows[i].Cells["材料品号"].Value.ToString();
                    String zcxm = (dataGridView6.Rows[i].Cells["转出项目号"].Value == null) ? "" : dataGridView6.Rows[i].Cells["转出项目号"].Value.ToString();
                    String zrxm = (dataGridView6.Rows[i].Cells["转入项目号"].Value == null) ? "" : dataGridView6.Rows[i].Cells["转入项目号"].Value.ToString();
                    String dbsl = (dataGridView6.Rows[i].Cells["数量"].Value == null) ? "0.00" : dataGridView6.Rows[i].Cells["数量"].Value.ToString();
                    String zjph = (dataGridView6.Rows[i].Cells["主件品号"].Value == null) ? "" : dataGridView6.Rows[i].Cells["主件品号"].Value.ToString();

                    strSql = "UPDATE DRPLA SET LA013='Y' WHERE LA001='" + clph + "' AND LA016='" + zjph + "' AND (LA002='" + zcxm + "' OR LA002='" + zrxm + "') AND LA011=" + dbsl + "";
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                        }
                        else
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }

                    strSql = "UPDATE MOCTB SET TB041=TB041+" + dbsl + " WHERE UDF03='" + zrxm + "' AND TB003='" + clph + "' AND EXISTS(SELECT * FROM MOCTA WHERE TA001=TB001 AND TA002=TB002 AND TA011<>'Y' AND TA011<>'y' AND TA006='" + zjph + "')";
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                        }
                        else
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                }
            }
            button12.PerformClick();
            MessageBox.Show("审核成功！", "软件提示");
        }

        private void dataGridView3_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if(PropertyClass.jiaose.IndexOf("业务员")<0)
            {
                MessageBox.Show("对不起,您无权编辑此数据!", "软件提示");
                dataGridView1.CurrentCell.Value = "";
                return;
            }
            String dqsz = (this.dataGridView3.CurrentCell.Value == null) ? "" : this.dataGridView3.CurrentCell.Value.ToString();
            if (dqsz != "")
            {
                if (e.ColumnIndex.Equals(this.dataGridView3.Columns["预计验货日"].Index))
                {
                    int zfsl = dataGridView3["订单号", dataGridView3.CurrentCell.RowIndex].Value.ToString().Trim().Length - 5;
                    string strSql1 = "UPDATE COPTC SET UDF01='" + dataGridView3["预计验货日", dataGridView3.CurrentCell.RowIndex].Value.ToString() + "' WHERE TC001+'-'+TC002=SUBSTRING('" + dataGridView3["订单号", dataGridView3.CurrentCell.RowIndex].Value.ToString() + "',1," + zfsl + ")";
                    try
                    {

                        if (db.ExecDataBySql1(strSql1) > 0)
                        {
                            MessageBox.Show("更新成功！", "软件提示");
                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                }
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                 
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView3.ColumnCount; i++)
                    {
                        if (dataGridView3.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView3.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView3.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView3.Columns.Count; k++)
                        {
                            if (dataGridView3.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView3.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView3.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }
    }
}
